package com.derbysoft.dswitch.adapter.ihg.common.service;

import com.derbysoft.dswitch.adapter.ihg.common.domain.ConfigInfo;
import com.derbysoft.dswitch.adapter.ihg.common.domain.FreeMealStrategy;
import com.derbysoft.dswitch.adapter.ihg.common.repository.ConfigRepository;
import com.derbysoft.dswitch.adapter.ihg.common.repository.FreeMealStrategyRepository;
import com.derbysoft.dswitch.adapter.ihg.common.utils.Constants;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * User: jason
 * Date: 2013-01-07
 */
@Component("freeMealMatchService")
public class FreeMealMatchServiceImpl implements FreeMealMatchService {
    @Autowired
    private ConfigRepository configRepository;

    @Autowired
    private FreeMealStrategyRepository freeMealStrategyRepository;

    @Override
    public FreeMealStrategy match(String channel, String hotel, String ratePlan, String roomType) {
        ConfigInfo configInfo = configRepository.loadUsable(Constants.KEY_MATCH_PRIORITY);
        if (configInfo == null || StringUtils.isBlank(configInfo.getValue())) {
            return null;
        }
        List<PriorityMarcher> priorityMarchers = new ArrayList<PriorityMarcher>();
        String[] values = StringUtils.split(configInfo.getValue(), Constants.SEMICOLON);
        for (String value : values) {
            PriorityMarcher priorityMarcher = new PriorityMarcher(StringUtils.trim(value));
            if (!contains(priorityMarchers, priorityMarcher)) {
                priorityMarchers.add(priorityMarcher);
            }
        }

        for (PriorityMarcher priorityMarcher : priorityMarchers) {
            FreeMealStrategy freeMealStrategy = loadFreeMealStrategy(channel, hotel, ratePlan, roomType, priorityMarcher);
            if (freeMealStrategy != null) {
                return freeMealStrategy;
            }
        }
        return null;
    }

    private boolean contains(List<PriorityMarcher> priorityMarchers, PriorityMarcher priorityMarcher) {
        for (PriorityMarcher marcher : priorityMarchers) {
            if (marcher.toString().equals(priorityMarcher.toString())) {
                return true;
            }
        }
        return false;
    }

    private FreeMealStrategy loadFreeMealStrategy(String channel, String hotel, String ratePlan,
                                                  String roomType, PriorityMarcher priorityMarcher) {
        String channelPattern = priorityMarcher.matchChannel(channel);
        String hotelPattern = priorityMarcher.matchHotel(hotel);
        String ratePlanPattern = priorityMarcher.matchRatePlan(ratePlan);
        String roomTypePattern = priorityMarcher.matchRoomType(roomType);
        return freeMealStrategyRepository.load(channelPattern, hotelPattern, ratePlanPattern, roomTypePattern);
    }

    public void setConfigRepository(ConfigRepository configRepository) {
        this.configRepository = configRepository;
    }

    public void setFreeMealStrategyRepository(FreeMealStrategyRepository freeMealStrategyRepository) {
        this.freeMealStrategyRepository = freeMealStrategyRepository;
    }
}
